layui.use(['element', 'jquery', 'layer', 'form', 'laypage', 'laydate'], function () {

    var element = layui.element;
    var form = layui.form;
    $ = layui.jquery;
    var layer = layui.layer;
    laypage = layui.laypage;
    laydate = layui.laydate;
    //按时间搜索
    laydate.render({
        elem: '#dateSelect' //指定元素
        , range: true
    });
    //数据搜索
    form.on('submit(formSearch)', function (data) {
        if (data.field.keyword == "" && data.field.ispay == "" && data.field.state == "") {
            layer.msg("搜索内容不能为空！");
        } else {
            getDataLists(1, data.field.ispay, data.field.state, data.field.keyword, "");
        }
        return false;
    });
    //重置搜索
    $("body").on("click", ".reset", function () {
        getDataLists(1, "", "", "", "");
    });
    var active = {
        add: function () {
            layer.open({
                type: 1 //此处以iframe举例
                , title: '<i class="layui-icon">&#xe608;</i> 添加用户'
                , area: ['390px', 'auto']
                , shade: 0.8
                , maxmin: false
                , content: $("#addUser")
                , btn: ['保存', '关闭'] //只是为了演示
                , yes: function () {
                    if ($("#name").val() == "") {
                        $("#name").focus();
                        layer.msg("用户名称不能为空！");
                        return false;
                    }
                    if ($("#mobile").val() == "") {
                        $("#mobile").focus();
                        layer.msg("手机号码不能为空！");
                        return false;
                    }
                    if ($("#password").val() == "") {
                        $("#password").focus();
                        layer.msg("密码不能为空！");
                        return false;
                    }
                    if ($("#password").val().length < 6) {
                        $("#password").focus();
                        layer.msg("密码不能少于6位！");
                        return false;
                    }
                    $.ajax({
                        url: adminurl + "/users/add",
                        data: $("#addUser").serialize(),
                        type: "POST",
                        dataType: "json",
                        beforeSend: function (request) {
                            index = layer.load(2, {
                                shade: [0.1, '#FFF'] //0.1透明度的白色背景
                            });
                        },
                        success: function (res) {
                            layer.close(index);
                            if (res.code == 1) {
                                layer.msg(res.msg);
                                setTimeout(function () {
                                    layer.closeAll();
                                    $("#addUser")[0].reset();
                                    getDataLists(1, "", "", "", "");
                                }, 500)
                            } else if (res.code == 1002) {
                                layer.msg(res.msg);
                                setTimeout(function () {
                                    location.href = adminurl + "/login";
                                }, 500)
                            } else {
                                layer.msg(res.msg);
                                return false;
                            }
                        },
                        error: function () {
                            layer.close(index);
                            layer.msg("网络请求错误，稍后重试！");
                            return false;
                        }
                    });
                }
                , btn2: function () {
                    layer.closeAll();
                }
                , zIndex: layer.zIndex //重点1
            });
        },
        black: function () {
            var _id = $(this).data("id");

            var _state = $(this).data("state");
            if (_state == 1) {
                var msg = "您确认将此用户取消黑名单吗？";
            } else {
                var msg = "您确认将此用户加入消黑名单吗？";
            }
            layer.confirm(msg, {
                btn: ['确认', '取消'] //按钮
            }, function () {
                $.ajax({
                    url: adminurl + "/users/black",
                    data: {
                        id: _id,
                        state: _state,
                        _token: _token,
                    },
                    type: "PUT",
                    dataType: "json",
                    beforeSend: function (request) {
                        index = layer.load(2, {
                            shade: [0.1, '#FFF'] //0.1透明度的白色背景
                        });
                    },
                    success: function (res) {
                        layer.close(index);
                        if (res.code == 1) {
                            console.log(1);
                            layer.closeAll();
                            layer.msg(res.msg);
                            setTimeout(function () {
                                layer.closeAll();
                                getDataLists(1, "", "", "", "");
                            }, 500)
                        } else if (res.code == 1002) {
                            layer.msg(res.msg);
                            setTimeout(function () {
                                location.href = adminurl + "/login";
                            }, 500)
                        } else {
                            layer.msg(res.msg);
                            return false;
                        }
                    },
                    error: function () {
                        layer.close(index);
                        layer.msg("网络请求错误，稍后重试！");
                        return false;
                    }
                });
            });
        }
    };
    $('body').on('click', '.handle', function () {

        var othis = $(this), method = othis.data('method');
        active[method] ? active[method].call(this, othis) : '';
    });

    getDataLists(1, "", "", "", ""); //初始化表格数据
    function getDataLists(page, keyword) {
        $.ajax({
            url: adminurl + "/users/lists",
            data: {
                page: page,
                keyword: keyword,
            },
            type: "get",
            dataType: "json",
            beforeSend: function () {
                index = layer.load(2, {
                    shade: [0.1, '#FFF'] //0.1透明度的白色背景
                });
            },
            success: function (res) {
                layer.close(index);
                if (res.code == 1) {
                    innerData = '';
                    if (res.data.count == 0) {
                        innerData += "<tr>";
                        innerData += '<td class="align-center" colspan="7">暂无数据</td>';
                        innerData += '</tr>';
                        laypage.render({elem: 'pages', count: res.data.count});
                    } else {
                        laypage.render({
                            elem: 'pages'
                            , count: res.count
                            , limit: 12
                            , layout: ['count', 'prev', 'page', 'next']
                            , jump: function (obj, first) {
                                //getRegionLists(obj.curr,obj);
                                //首次不执行
                                if (!first) {
                                    $.ajax({
                                        url: adminurl + "/users/lists",
                                        data: {
                                            page: obj.curr,
                                            keyword: keyword,
                                        },
                                        type: "get",
                                        dataType: "json",
                                        beforeSend: function (request) {
                                            index = layer.load(2, {
                                                shade: [0.1, '#FFF'] //0.1透明度的白色背景
                                            });
                                        },
                                        success: function (res) {
                                            layer.close(index);
                                            if (res.code == 1) {
                                                innerData = '';
                                                if (res.data.count == 0) {
                                                    innerData += "<tr>";
                                                    innerData += '<td class="align-center" colspan="8">暂无数据</td>';
                                                    innerData += '</tr>';
                                                } else {
                                                    floatDataTpl(res.data.lists, page);
                                                }
                                                $('#listbox').html(innerData);
                                                form.render();
                                            } else if (res.code == 1002) {
                                                layer.msg(res.msg);
                                                setTimeout(function () {
                                                    location.href = adminurl + "/login";
                                                }, 500)
                                            } else {
                                                layer.msg(res.msg);
                                                return false;
                                            }
                                        },
                                        error: function () {
                                            layer.close(index);
                                            layer.msg("网络请求错误，稍后重试！");
                                            return false;
                                        }
                                    });
                                } else {
                                    floatDataTpl(res.data.lists, page);
                                }
                            }
                        });
                    }
                    $('#listbox').html(innerData);
                    form.render();
                } else if (res.code == 1002) {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        location.href = adminurl + "/login";
                    }, 500)
                } else {
                    layer.msg(res.msg);
                    return false;
                }
            },
            error: function () {
                layer.close(index);
                layer.msg("网络请求错误，稍后重试！");
                return false;
            }
        });
    }

    //数据格式化模板
    function floatDataTpl(data, page) {
        layui.each(data, function (index, item) {
            innerData += "<tr>";
            innerData += '<td>' + ((index * page) + 1) + '</td>';
            if (item.nickname) {
                innerData += '<td>' + item.nickname + '</td>';
            } else {
                innerData += '<td>未设置</td>';
            }
            innerData += '<td>' + item.mobile + '</td>';
            innerData += '<td>' + item.user_level_name + '</td>';
            innerData += '<td>￥0.00</td>';
            innerData += '<td>';
            if (is_edit) {
                innerData += setStateStr;//+'<a data-method="delete" class="layui-btn layui-btn-small layui-btn layui-btn-danger handle" href="javascript:;" ><i class="layui-icon"></i>删除</a>';
            }
            innerData += '</td></tr>';
        });
    }
})